package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author dxt
 * @create 2025-07-16-14:42
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 客户列表分页（名称模糊查询）
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public Map<String, Object> getCustomerListPage(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> result = new HashMap<>();
        int start = (page - 1) * rows;
        List<Customer> list = customerDao.selectCustomerListPage(start, rows, customerName);
        Integer count = customerDao.selectCount();
        result.put("total", count);
        result.put("rows", list);
        return result;
    }

    /**
     * 保存或更新客户信息
     * @param customerId
     * @param customer
     */
    @Override
    public void saveOrUpdate(Integer customerId, Customer customer) {
        if (customerId == null){
            customerDao.insert(customer);
        }else {
            customerDao.update(customerId, customer);
        }
    }

    /**
     * 删除客户信息
     * @param ids
     */
    @Override
    public void delete(String ids) {
        String[] customerIdList = ids.split(",");
        for (String customerId : customerIdList) {
            customerDao.delete(customerId);
        }
    }

    /**
     * 查询客户下拉框数据
     * @param customerName 客户名称
     * @return
     */
    @Override
    public List<Customer> getComboboxList(String customerName) {
        return customerDao.selectComboboxList(customerName);
    }
}
